/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jsrpc.core;

import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Properties;

/**
 *
 * @author neo
 */
public class DefaultObjectToStringHandler extends ObjectToStringHandler {

    public String toString(Object obj, Properties prop) {
        StringBuilder s = new StringBuilder();
        s.append("reObj={");
        try {
            Method[] methods = obj.getClass().getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                Method method = methods[i];
                //System.out.println("return Method["+ method.getName() +"]");
                if (method.getName().startsWith("get")) {
                    String mName = method.getName().substring(3);
                    mName = mName.substring(0, 1).toLowerCase() + mName.substring(1);
                    s.append("\"").append(mName).append("\": ");
                    Object reObj = method.invoke(obj, new Object[]{});
                    String sCase = null;
                    if( reObj == null ) {
                        sCase = "java.lang.String";
                    } else {
                        sCase = reObj.getClass().getName();
                    }
                    System.out.println("scase["+ sCase +"]");
                    int iCase = Integer.parseInt(prop.getProperty(sCase + ".return.ptype"));
                    switch (iCase) {
                        case 0:
                            break;
                        case 1:
                            String sval = (String) reObj;
                            if( sval == null ) {
                                sval = "";
                            } else {
                                if( sval.length() == 0 ) {
                                    sval = "";
                                }
                            }
                            s.append("\"").append(sval).append("\"");
                            break;
                        case 2: {
                            int val = ((Integer) reObj).intValue();
                            s.append(val);
                        }
                        break;
                        case 3: { //long
                            long val = ((Long) reObj).longValue();
                            s.append(val);
                        }
                        break;
                        case 4: { //double
                            double val = ((Double) reObj).doubleValue();
                            s.append(val);
                        }
                        break;
                        case 5: { //float
                            float val = ((Float) reObj).floatValue();
                            s.append(val);
                        }
                        break;
                        case 6: { //Date
                            java.util.Date dm = ((java.util.Date) reObj);
                            SimpleDateFormat sd = new SimpleDateFormat(prop.getProperty("date.format"));
                            String date = sd.format(dm);
                            s.append("\"").append(date).append("\"");
                        }
                        break;
                        case 7: { //boolean
                            boolean val = ((Boolean) reObj).booleanValue();
                            s.append(val);
                            s.append(reObj);
                        }
                        break;
                        case 8: { //Date
                            
                            Timestamp tsp = ((Timestamp) reObj);
                            java.util.Date dm = new java.util.Date(tsp.getTime());
                                    SimpleDateFormat sd = new SimpleDateFormat(prop.getProperty("date.format"));
                            String date = sd.format(dm);
                            s.append("\"").append(date).append("\"");
                        }
                        break;
                        default:
                            s.append("\"").append(reObj.toString()).append("\"");
                            break;
                    }
                    s.append(",");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        s.delete(s.length() - 1, s.length());
        s.append("};");
        return s.toString();
    }
//    private String toMethodName(String name) {
//        int index = name.lastIndexOf(".");
//        String cname = name.substring(index + 1);
//        StringBuffer m = new StringBuffer(cname);
//        StringBuffer ch = new StringBuffer(cname.substring(0, 1).toLowerCase());
//        boolean cf = false;
//        for (int i = 1; i <
//                m.length(); i++) {
//            char c = m.charAt(i);
//            if (cf == true) {
//                ch.append(Character.toUpperCase(c));
//                cf =
//                        false;
//            } else {
//                if (c == '_') {
//                    cf = true;
//                } else {
//                    ch.append(c);
//                }
//
//            }
//        }
//        return ch.toString();
//    }
}
